package com.infonautics.dao.imp;

import java.util.List;

import com.hibernate.HibernateDao;
import com.hibernate.Userinfo;
import com.hibernate.Unit;
import com.hibernate.Userrole;
import com.infonautics.dao.IUserinfoDao;
import com.infonautics.page.PageBean;
import com.system.dao.ISystemDao;

public class IUserinfoDaoImp implements ISystemDao<Userinfo>,IUserinfoDao{

	HibernateDao<Userinfo> userinfoDao = new HibernateDao<Userinfo>();
	HibernateDao<Unit> unitDao = new HibernateDao<Unit>();
	HibernateDao<Userrole> userroleDao = new HibernateDao<Userrole>();
	
	@Override
	public PageBean<Userinfo> doList(int pageSize, int page,
			int id) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void doAdd(Userinfo object) throws Exception {
		if(object != null){
			userinfoDao.addObject(object);
		}
	}

	@Override
	public void doModfiy(Userinfo object) throws Exception {
		if(object != null){
			userinfoDao.updateObject(object);
		}
	}

	@Override
	public void doDelete(int id) throws Exception {
		userinfoDao.deleteObject(id, Userinfo.class);
		
	}

	@Override
	public Userinfo doOne(int id) throws Exception {
		
		return userinfoDao.getObject(id, Userinfo.class);
	}

	@Override
	public Userinfo doLogin(String username, String password)  throws Exception{
		String hql = "from Userinfo where username='"+username+"' and password='"+password+"'";
		List<Userinfo> userinfoList = userinfoDao.getListObjects(hql);
		Userinfo userinfo = null;
		if(userinfoList.size()>0){
			userinfo = userinfoList.get(0);
		}
		return userinfo;
	}

	@Override
	public PageBean<Userinfo> doUserinfoList(int pageSize, int page)  throws Exception{
		final String hql = "from Userinfo order by id desc";
		int allRow = userinfoDao.getAllRowCount("select count(*) from Userinfo"); // 
		int totalPage = PageBean.countTotalPage(pageSize, allRow);
		final int offset = PageBean.countOffset(pageSize, page);
		final int length = pageSize;
		final int currentPage = PageBean.countCurrentPage(page);
		List<Userinfo> list = userinfoDao.getObjects(hql, offset,length);
	
		PageBean<Userinfo> pageBean = new PageBean<Userinfo>();
		pageBean.setPageSize(pageSize);
		pageBean.setCurrentPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();
		return pageBean;
	}

	@Override
	public List<Unit> doUnitList()  throws Exception{
		String hql = "from Unit order by id desc";
		List<Unit> unitList = unitDao.getListObjects(hql);
		return unitList;
	}

	@Override
	public List<Userrole> doUserroleList()  throws Exception{
		String hql = "from Userrole order by id desc";
		List<Userrole> roleList = userroleDao.getListObjects(hql);
		return roleList;
	}

	@Override
	public PageBean<Userinfo> doUserinfoList(int pageSize, int page, String sql)
			throws Exception {
		final String hql = "from Userinfo where 1=1 "+ sql +" order by id desc";
		int allRow = userinfoDao.getAllRowCount("select count(*) from Userinfo where 1=1 "+ sql); // 
		int totalPage = PageBean.countTotalPage(pageSize, allRow);
		final int offset = PageBean.countOffset(pageSize, page);
		final int length = pageSize;
		final int currentPage = PageBean.countCurrentPage(page);
		List<Userinfo> list = userinfoDao.getObjects(hql, offset,length);
	
		PageBean<Userinfo> pageBean = new PageBean<Userinfo>();
		pageBean.setPageSize(pageSize);
		pageBean.setCurrentPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();
		return pageBean;
	}

}
